package com.wu.shiro.jwt;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;

/**
 * @author wuyanshen
 * @date 2019-01-15 8:15 PM
 * @discription 描述
 */
public class SubjectFactory extends DefaultWebSubjectFactory {
    private final DefaultSessionStorageEvaluator storageEvaluator;

    public SubjectFactory(DefaultWebSecurityManager securityManager) {
        this.storageEvaluator = (DefaultSessionStorageEvaluator)((DefaultSubjectDAO)securityManager.getSubjectDAO()).getSessionStorageEvaluator();
    }

    @Override
    public Subject createSubject(SubjectContext context) {
        this.storageEvaluator.setSessionStorageEnabled(Boolean.TRUE);
        AuthenticationToken token = context.getAuthenticationToken();
        if(token instanceof JwtToken){
            // 不创建 session
            context.setSessionCreationEnabled(false);
            // 不持久化session
            this.storageEvaluator.setSessionStorageEnabled(Boolean.FALSE);
        }
        return super.createSubject(context);
    }
}
